var index = 1, size = 20, key = '', pager = null;

var config = {
    table: {
        toolbar:'#table-auth-btn',
        pageSize: size,
        onClickRow:function () {
            optRow = component.datagrid.getRowData('#table-list');
            optIndex = component.datagrid.getDataRow('#table-list');
        },
        frozenColumns:[
            [
                {field:'loginName',title:'操作账号', align:'center',width:150 },
                {field:'ip',title:'来源', align:'center' },
                {field:'method',title:'请求类型', align:'center' },
                {field:'time',title:'耗时(ms)', align:'center' },
                {field:'error',title:'请求状态', align:'center',
                    formatter:function (val, row, index) {
                        switch (parseInt(val)){
                            case 1:
                                return '<a href="#" style="color:red;">失败</a>';
                            case 3:
                                return '<a href="#" style="color:red;">错误</a>';
                            case 0:
                                return '<a href="#" style="color:green;">成功</a>';
                            case 9:
                                return '<a href="#" style="color:red;">异常</a>';
                        }
                    }
                },
                {field:'message',title:'响应消息', align:'left' },
            ]
        ],
        columns:[
            [
                {field:'classMethod',title:'请求函数', align:'left' },
                {field:'url',title:'请求URL', align:'left'},
                {field:'createTime',title:'请求时间', align:'left',
                    formatter:function (val, row, index) {
                        return kit.time.timestamp2time(val);
                    }
                },
                {field:'token',title:'token', align:'center',
                    formatter:function (val, row, index) {
                        return "<a href=\"#\" style=\"color:green;\" onclick=\"opt.read_token('"+val+"');\">解析</a>";
                    }
                },
                {field:'param',title:'请求参数', align:'center',
                    formatter:function (val, row, index) {
                        if(type == 3){
                            return "不可见";
                        }else{
                            return "<a href=\"#\" style=\"color:green;\" onclick=\"opt.read_param("+index+");\">查看</a>";
                        }
                    }
                },
            ]
        ]
    },
    label: {
        width:500,
        height:400,
        left: (kit.wh().width - 500) / 2
    },
}
$(function () {
    action.get();
})

var opt = {
    search:function (_event) {
        var obj = kit.form.get('#query-form')
        key = obj.loginName;
        index = 1;
        action.get();
    },
    refresh:function () {
        kit.form.clean('#query-form');
        index = 1;
        key = '';
        action.get();
    },
    read_token: function (_token) {
        action.readToken(_token);
    },
    read_param: function (index) {
        $('#table-list').datagrid('selectRow', index);
        var row = $('#table-list').datagrid('getSelected');

        var _param = eval('(' + row.param + ')');
        var codeHtml = '<pre>'+JSON.stringify(_param, null, 4)+'</pre>';
        config.label.title = "请求参数解析";
        config.label.height = 400;
        component.dialog.init('#opt-form-blank', config.label);
        component.dialog.open('#opt-form-blank');
        $('#opt-form-blank').html(codeHtml);
    }
}
var action = {
    get: function () {
        var param = {
            url: constant.route.log + '/' + index + '-' + size + '-' + type + '-' + key
        };
        var request = http.get(param);
        request.done(function (d) {
            config.table.data = d.result;
            component.datagrid.init('#table-list', config.table);
            pager = component.datagrid.getPager('#table-list');
            pager.pagination({
                onSelectPage:function (pageNumber, pageSize) {
                    index = pageNumber;
                    size = pageSize;
                    action.page();
                },
                onBeforeRefresh:function (pageNumber, pageSize) {
                    action.page();
                }
            })
        })
    },
    page:function () {
        var param = {
            url: constant.route.log + '/' + index + '-' + size + '-' + type + '-' + key
        };
        var request = http.get(param);
        request.done(function (d) {
            config.table.data = d.result;
            component.datagrid.load('#table-list', d.result);
        })
    },
    readToken: function (_token) {
        var param = {
            url: constant.route.log,
            data: {token:_token}
        };
        var request = http.post(param);
        request.done(function (d) {
            var codeHtml = '<pre>'+JSON.stringify(d.result, null, 4)+'</pre>';
            config.label.title = "token解析";
            config.label.height = 400;
            component.dialog.init('#opt-form-blank', config.label);
            component.dialog.open('#opt-form-blank');
            $('#opt-form-blank').html(codeHtml);
        })
    }
}